import http from 'http';
import path from 'path';
import fs from 'fs';
import url from 'url';


let dir = path.resolve('.');
console.log(dir);

let a = http.createServer((req, res) => {
    console.log(url.format(req.url));

    if (req.url == '/index') {
        fs.readFile('index.html', 'utf-8', (err, data) => {
            if (err) {
                console.log(req.url);

            } else {
                res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
                res.end(data);
            }
        })
    } else if (req.url == '/b') {
        console.log(111);
        res.writeHead(200, { location: 'http://baidu.com' });
        res.end("无此页面")
    } else {
        console.log(222);
        res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
        res.end("无此页面")
    }

})



a.listen(8080, "localhost", () => {
    console.log("监听成功");
})

console.log(`服务器运行在http://localhost:8080`);